package com.github.ykrank.alipayxposed.app;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import org.jetbrains.annotations.NotNull;

public class AppConfigHelper {

    private static final String KEY_MINUTE = "KEY_MINUTE_INTERVAL";
    private static final String KEY_SECOND = "KEY_SECOND_INTERVAL";
    private static final String KEY_API_URL = "KEY_API_URL";
    private SharedPreferences sharedPreferences;

    private AppConfigHelper(Context context) {
        sharedPreferences = context.getSharedPreferences("App_Config", Context.MODE_PRIVATE);
    }

    public static AppConfigHelper getInstance(Context context) {
        return new AppConfigHelper(context);
    }


    /**
     * 时间间隔以分钟和秒作为单位  比如 12m40s
     *
     * @param minuteInterval 分钟
     * @param secondInterval 秒
     */
    @SuppressLint("ApplySharedPref")
    public void saveTimeInterval(int minuteInterval, int secondInterval) {
        sharedPreferences.edit().putInt(KEY_MINUTE, minuteInterval).putInt(KEY_SECOND, secondInterval)
                .commit();
    }


    /**
     * @return 单位是：秒
     */
    public int getTimeInterval() {
        int minute = sharedPreferences.getInt(KEY_MINUTE, 0);
        int second = sharedPreferences.getInt(KEY_SECOND, 10);
        return minute * 60 + second;
    }

    public void saveApiAddress(@NotNull String apiAddress) {
        sharedPreferences.edit().putString(KEY_API_URL, apiAddress)
                .apply();
    }

    public String getApiAddress() {
        return sharedPreferences.getString(KEY_API_URL, "http://www.ji7test.com/api_notify/alipay2");
    }
}
